package com.ken.app_manager;

import com.google.gson.Gson;
import com.ken.app_manager.base.OnBaseCallback;
import com.ken.app_manager.bean.BaseBean;
import com.ken.app_manager.bean.UserBean;
import com.ken.app_manager.server.ControllerServer;
import com.ken.app_manager.server.ControllerServerImpl;
import com.ken.app_manager.utils.Config;
import com.ken.app_manager.core.JdbcTemplate;
import com.ken.app_manager.core.PreparedStatementCreator;
import com.ken.app_manager.core.RowCallbackHandler;
import com.ken.app_manager.utils.JsonReader;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@javax.servlet.annotation.WebServlet(name = "MyServet", urlPatterns = {"/myservet"})
public class MyServet extends javax.servlet.http.HttpServlet {
    ControllerServer controllerServer = new ControllerServerImpl();

    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request, response);
    }


    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        System.out.println("服务器启动成功");
        request.setCharacterEncoding("utf-8");
        response.setContentType(Config.json_ContentType);


        System.out.println("getRequestURI:"+request.getRequestURI());
        JSONObject jsonObject = JsonReader.receivePost(request);
        UserBean userBean = new Gson().fromJson(jsonObject.toString(), UserBean.class);

        System.out.println("插入的数据"+ userBean.toString());

        String action = request.getParameter("action");
        PrintWriter out = response.getWriter();
        JSONObject object = new JSONObject();
        BaseBean baseBean = new BaseBean();
        List<UserBean> list = new ArrayList<>();

        switch (action.toLowerCase()) {
            case "search":



                baseBean.setMessage("success");
                baseBean.setCode(200);
                JSONArray array = JSONArray.fromObject(list);
                String jsonStr = array.toString();
                object.put("data", jsonStr);
                break;

            case "update":
//                更新操作
                break;
            case "add":
//              添加操作

                controllerServer.register(userBean, new OnBaseCallback() {
                    @Override
                    public void onSuccess(String result) {
                        baseBean.setMessage("success");
                        baseBean.setCode(200);
                    }

                    @Override
                    public void onError(int code, String message) {
                        baseBean.setMessage(message);
                        baseBean.setCode(code);
                        System.out.printf("code:%d   message%s", code, message);
                    }
                });
                break;
            case "delete":

                break;
            default:
                baseBean.setMessage("参数传递错误");
                baseBean.setCode(500);
                break;


        }

        object.put("message", baseBean.getMessage());
        object.put("code", baseBean.getCode());
        out.write(object.toString());
//        out.write(FormatUtil.printJson(object.toString()));
        out.close();
    }

}
